<template>
  <div class="container">
    <h3>蜂农越界报警消息</h3>
    <!-- 消息展示区域 -->
    <div ref="messageListRef" class="message-list box-container">
      <div v-for="(msg, index) in websocketStore.messages" :key="index" :class="`message ${msg.type}`">
        <!-- 报警消息：替换为“alarm”图标（假设项目有该报警专用图标） -->
        <svg-icon v-if="msg.type === 'alarm'" icon-class="alarm" class="message-icon" />
        <!-- 系统消息：信息图标 -->
        <svg-icon v-else icon-class="info" class="message-icon" />
        <span class="time">{{ msg.time }}</span>
        <span class="content">{{ msg.content }}</span>
      </div>
    </div>
  </div>
</template> 

<script setup name="WebSocketClient">
import { ref, onMounted, watch } from 'vue'
import { useWebSocketStore } from '@/store/modules/websocket'
import useUserStore from '@/store/modules/user'


// 全局状态
const websocketStore = useWebSocketStore()
const userStore = useUserStore()
const messageListRef = ref(null)

// 监听消息变化，自动滚动到底部
watch(
  () => websocketStore.messages,
  () => scrollToBottom(),
  { deep: true }
)

// 页面挂载时滚动到底部
onMounted(() => {
  scrollToBottom()
})

// 自动滚动到底部
const scrollToBottom = () => {
  if (messageListRef.value) {
    messageListRef.value.scrollTop = messageListRef.value.scrollHeight
  }
}

// 监听登录状态变化（核心：全局连接管理）
watch(
  () => userStore.token,
  (newToken) => {
    if (newToken) {
      // 登录后建立连接
      websocketStore.connect(userStore)
    } else {
      // 退出登录关闭连接
      websocketStore.close()
    }
  },
  { immediate: true } // 初始化时执行
)


</script>

<style scoped>
/* 容器：卡片化 + 渐变背景 + hover 阴影增强 */
.container {
  position: absolute;
  top: 1px;
  right: 0;
  bottom: 0;
  /* max-width: 800px; */
  width: 100%;
  padding: 20px;
  background: linear-gradient(to bottom, #fdfcfb, #f2eeec);
  box-sizing: border-box;
  overflow: hidden;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: box-shadow 0.3s ease;
}

.container:hover {
  box-shadow: 0 6px 30px rgba(0, 0, 0, 0.12);
}

/* 标题：增大字号 + 底部边框区分 */
h3 {
  margin: 0 0 15px 0;
  padding: 0 0 10px 0;
  font-size: 22px;
  font-weight: 600;
  color: #333;
  text-align: center;
  border-bottom: 2px solid #eaeaea;
}

/* 消息列表：内层卡片 + 内阴影 */
.message-list.box-container {
  border-radius: 8px;
  height: calc(100% - 60px);
  margin-top: 10px;
  padding: 10px;
  overflow-y: auto;
  box-sizing: border-box;
  background-color: #fff;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.05);
}

/* 消息项：统一布局 + hover 上浮 + 类型区分 */
.message {
  margin: 8px 0;
  padding: 12px;
  border-radius: 6px;
  transition: all 0.2s ease;
  display: flex;
  flex-direction: column;
}

.message:hover {
  transform: translateY(-2px);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.06);
}

/* 系统消息：浅蓝灰边框 + 柔和背景 */
.message.system {
  background-color: #f8fafc;
  border-left: 3px solid #94a3b8;
}

/* 报警消息：红色边框 + 脉冲动画（强调紧急性） */
.message.alarm {
  background-color: #fff5f5;
  border-left: 3px solid #f5222d;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(245, 34, 45, 0.2);
  }

  70% {
    box-shadow: 0 0 0 8px rgba(245, 34, 45, 0);
  }

  100% {
    box-shadow: 0 0 0 0 rgba(245, 34, 45, 0);
  }
}

/* 消息图标：视觉区分类型 */
.message-icon {
  width: 20px;
  height: 20px;
  margin-right: 8px;
  vertical-align: middle;
  display: inline-block;
}

.message.alarm .message-icon {
  color: #f5222d;
}

.message.system .message-icon {
  color: #94a3b8;
}

/* 时间与内容：排版优化 */
.time {
  color: #666;
  margin-right: 8px;
  font-size: 0.85em;
  opacity: 0.8;
  display: inline-block;
}

.content {
  font-size: 1em;
  line-height: 1.5;
  margin-top: 4px;
}

/* 滚动条：简洁美化 */
.message-list.box-container::-webkit-scrollbar {
  width: 5px;
}

.message-list.box-container::-webkit-scrollbar-track {
  background: transparent;
}

.message-list.box-container::-webkit-scrollbar-thumb {
  background: #d1d5db;
  border-radius: 5px;
}

.message-list.box-container::-webkit-scrollbar-thumb:hover {
  background: #9ca3af;
}
</style>